﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace Fish_Atlantis.Account
{
    public partial class Login1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btn_sub_Click(object sender, EventArgs e)
        {
            string username = this.Text1.Value.ToString();
            string userpwd = this.Password1.Value.ToString();


            string str = "select * from Users where UserName='" + username + "' and PassWord='" + userpwd + "'";

            DataSet ds = Helper.GetDataSet(str, "Users");


            if (ds.Tables[0].Rows.Count <= 0)
            {

                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登陆失败！用户名或密码错误');</script>");
                return;
            }
            else
            {
                HttpCookie UserNameCookie = Request.Cookies["UserNameCookie"];
                HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
                if (this.Checkbox1.Checked == true)
                {
                    if (UserNameCookie == null || UserPasswordCookie.Values["UserNameCookie"] != username)
                    {
                        UserNameCookie = new HttpCookie("UserNameCookie");
                        UserNameCookie.Values.Add("UserName", username);
                        UserNameCookie.Expires = DateTime.Now.AddDays(30);
                        Response.Cookies.Add(UserNameCookie);
                    }
                    if (UserPasswordCookie == null || UserPasswordCookie.Values["UserPassword"] != userpwd)
                    {
                        UserPasswordCookie = new HttpCookie("UserPasswordCookie");
                        UserPasswordCookie.Values.Add("UserPassword", userpwd);
                        UserPasswordCookie.Expires = DateTime.Now.AddDays(30);
                        Response.Cookies.Add(UserPasswordCookie);
                    }
                }
            }
            Session["UserID"] = ds.Tables[0].Rows[0]["UserID"].ToString();
            Session["UserName"] = ds.Tables[0].Rows[0]["UserName"].ToString();
            Session["User_Type"] = ds.Tables[0].Rows[0]["User_Type"].ToString();
            string type=Session["User_Type"].ToString();
            if(type=="1")
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份是customer');location.href='/CustomerPanel/MainPage.aspx';</script>");
            else if(type=="2")
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份是supplier');location.href='/SupplierPanel/MainPage.aspx';</script>");
            else if(type=="3")
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份是professor');location.href='/ProfessorPanel/MainPage.aspx';</script>");
            else
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份不能识别');location.href='/Default.aspx';</script>");



        }

    }

}
